# -*- coding: utf-8 -*-
"""
Created on Wed Apr  6 14:15:19 2022

@author: Administrator
"""
import numpy as np
from sklearn import datasets
#iris数据集
# d=datasets.load_iris()
# features=d.data
# prices=d.target
#泰坦尼克号
features=np.load('titanic_feature.npy')
prices=np.load('titanic_target.npy')
# 清洗和分割数据
from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier
X, X_test1, y, y_test1 = train_test_split(features, prices, test_size=0.3,random_state=50)
from sklearn.model_selection import GridSearchCV

param_grid = {'min_samples_split':np.arange(2, 10),'max_depth':np.arange(2, 10)}
clf = DecisionTreeClassifier(splitter='best',random_state=50)
GS = GridSearchCV(clf, param_grid, cv=10)
GS.fit(X, y)
print(GS.best_params_)
print(GS.best_score_)
clf = DecisionTreeClassifier(max_depth=GS.best_params_['max_depth'],
                            min_samples_split=GS.best_params_['min_samples_split']
                            ,random_state=50)

clf.fit(X, y)
y_score=clf.score(X_test1,y_test1)
print(y_score)
from sklearn.tree import export_graphviz    # 导入的是一个函数
with open('E:/jupyter/iris调优1.dot', 'w', encoding='utf-8') as f:
    f = export_graphviz(clf,  out_file=f,filled=True,rounded=True)
